package com.ifast.common.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;

public class InterfaceSendUtil {
    public static  String interfaceOne(String a){
        //测试环境地址
        //String url="http://59.110.11.82:9999/admin/funinchat/arenaCrawler/importQuestion";
        //正式环境地址
        String url = "http://cms.funinchat.com/admin/funinchat/arenaCrawler/importQuestion";
        final String CONTENT_TYPE_TEXT_JSON = "text/json";
        CloseableHttpClient client = InterfaceSendUtil.httpclient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");

        StringEntity se = null;
        se = new StringEntity(a,"Utf-8");
        se.setContentType(CONTENT_TYPE_TEXT_JSON);
        se.setContentEncoding("UTF-8");
        httpPost.setEntity(se);

        CloseableHttpResponse response2 = null;

        try {
            response2 = client.execute(httpPost);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity entity2 = null;
        entity2 = response2.getEntity();
        String s2 = null;
        try {
            s2 = EntityUtils.toString(entity2, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(s2);
        return s2;
    }
    public static CloseableHttpClient httpclient() {
        CloseableHttpClient httpclient = HttpClients.custom().setRetryHandler( myRetryHandler ).build();
        return httpclient;
    }

    public static HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
        @Override
        public boolean retryRequest(
                IOException exception,
                int executionCount,
                HttpContext context) {
            if (executionCount >= 5) {
                // Do not retry if over max retry count
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                return true;
            }
            if (exception instanceof SSLHandshakeException) {
                return false;
            }
            if (exception instanceof InterruptedIOException) {
                // Timeout
                return false;
            }
            if (exception instanceof UnknownHostException) {
                // Unknown host
                return false;
            }
            if (exception instanceof ConnectTimeoutException) {
                // Connection refused
                return false;
            }
            if (exception instanceof SSLException) {
                // SSL handshake exception
                return false;
            }
            HttpClientContext clientContext = HttpClientContext.adapt( context );
            HttpRequest request = clientContext.getRequest();
            boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
            if (idempotent) {
                return true;
            }
            return false;
        }

    };
}
